//tslint:disable
import { Component, Input, OnInit, AfterViewChecked } from '@angular/core';
import { Router } from '@angular/router';
import { Platform } from '@ionic/angular';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { ProcessLoginDataService } from 'src/app/shared/service/process-login-data.service';
import { ShareService } from 'src/app/shared/service/share.service';

import { AppGlobalService } from '../../shared/service/app-global.service';
import { EventService } from '../../shared/service/event.service';
import { HttpService } from '../../shared/service/http.service';

@Component({
    selector: 'app-mine',
    templateUrl: './mine.component.html',
    styleUrls: ['./mine.component.scss']
})
export class MineComponent implements OnInit {

    constructor(
        private platform: Platform,
        public router: Router,
        private appGlobalService: AppGlobalService,
        private eventService: EventService,
        private processLoginData: ProcessLoginDataService,
        private pageModalService: PageModalService,
        private shareService: ShareService,
        private http: HttpService
    ) {
    }
    @Input() tagData: any;
    ifLogin: boolean;

    data: any;
    targetObj: any = {
        forward: {},  // 跳转相关参数
        path: '',     // 跳转路由
        bat: [],        // 记录每次跳转的参数
        way: ''
    };
    userData: any;
    appTypeCustom: any;
    getPhoneSubmitId: any; // 获取手机号码的submitId
    loginSubmitId: any; // 登录的submitId
    ngOnInit() {
        const bodyStyles = window.getComputedStyle(document.body);
        this.appTypeCustom = bodyStyles.getPropertyValue('--appTypeCustom').trim().replace('"', '').replace('"', '');
        // console.log('page----------> login', this.tagData);
        // // 广播
        // this.eventService.eventEmit.on('refreshUserInfo', () => {
        //     console.log('refreshUserInfo----------->>login');
        //     const tmpUserInfo = JSON.parse(localStorage.getItem('userInfo'));
        //     this.tagData.data.userName = tmpUserInfo.nickname;
        //     this.tagData.data.userAvatar = tmpUserInfo.avatar_url;
        // });
        this.ifLogin = JSON.stringify(this.tagData.data) !== '{}';
        // console.log('message----->islogin =', this.tagData);

        this.userData = this.tagData.data.userData;
    }

    link(obj) {
        const params = {
            pageId: obj.target,
            params: obj.param,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(params);
    }

    linkToUser(obj) {
        const params = {
            pageId: obj.link.target,
            params: obj.link.params,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(params);
    }

    back() {

    }

    /**
     * 点击头像跳转
     *  MinePage
     */
    navLink() {
        // 只有手机端才会发起极光认证
        if (localStorage.getItem('client') === 'APP') {
            // 未登录才会跳转到一键登录
            if (!this.ifLogin && this.tagData.notLogin && this.tagData.notLogin.params && this.tagData.notLogin.params.length > 0) {
                // 是否跳转到一键登录
                let isJVerificationLogin = false;
                let params = {
                    sessionId: localStorage.getItem("sessionId"),
                    pageId: '',
                    params: {}
                };
                for (let index = 0; index < this.tagData.notLogin.params.length; index++) {
                    const element = this.tagData.notLogin.params[index];
                    if (element.name === 'oneLogin') {
                        isJVerificationLogin = true;
                        params.pageId = element.value;
                        break;
                    }
                }

                // 当前为极光登录
                if (isJVerificationLogin) {
                    this.processLoginData.jVerificationLogin(params, this.getPhoneSubmitId, this.loginSubmitId, (msg) => {
                        // 其他登录
                        if (window['otherButton'] !== null && window['otherButton'] !== undefined) {
                            const link = window['otherButton'];
                            this.pageModalService.openPage(link);
                            window['otherButton'] = null;
                        } else if (window['wxlinkButton'] !== null && window['wxlinkButton'] !== undefined) {
                            const link = window['wxlinkButton'];
                            this.shareService.gotoAutoLoginWithWechatOpenId(this.appGlobalService.clientType === 'js' ? 2 : 1, (res) => {
                                if (res && res.isLogin !== undefined && res.isLogin) {
                                    // 接口还未返回该参数故用默认值
                                    this.processLoginData.processLoginData(this.appGlobalService.loginRes, this.appGlobalService.loginRes.data.account, this.appGlobalService.loginRes.params);
                                } else if (res && res.isLogin !== undefined && !res.isLogin) {
                                    console.log('还未绑定!');
                                    if (link) {
                                        // 跳转到普通登录页面
                                        this.pageModalService.openPage(link);
                                    }
                                }
                            });
                            window['wxlinkButton'] = null;
                        } else {
                            if (msg === 6002) { // 用户主动取消登录
                            } else {
                                this.normalLogin();
                            }
                        }
                    });
                } else {
                    this.normalLogin();
                }
            } else {
                this.normalLogin();
            }
        } else {
            this.normalLogin();
        }
    }

    normalLogin() {
        let linkParams = {};
        if (this.tagData && this.tagData.login && this.tagData.login.params) {
            this.tagData.login.params.forEach(element => {
                linkParams[element.name] = element.value;
            });
        }
        if (this.ifLogin) {
            linkParams['_id'] = this.tagData.data._id;
        }
        const params = {
            pageId: '',
            params: linkParams,
            sessionId: localStorage.getItem('sessionId')
        };
        params.pageId = this.ifLogin ? this.tagData.login.target : this.tagData.notLogin.target;
        this.appGlobalService.goToForward(params);
    }


    gotoSittings() {
        const params = {
            pageId: this.tagData.setting.link.target,
            params: this.tagData.setting.link.param,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(params);
    }

    call(event, data) {
        if (event) {
            event.stopPropagation();
        }
        if (data && data.setting && data.setting.link && data.setting.link.params && data.setting.link.params.superiorName && data.setting.link.params.superiorMobile) {
            this.pageModalService.call({ name: data.setting.link.params.superiorName, phone: data.setting.link.params.superiorMobile });
        }

    }

    gotoPage(event,link) {

        if(event){
            event.stopPropagation();
        }
        this.pageModalService.openPage(link);
    }


}
